//
//  UGZRCpay.m
//  onebuycheckstand
//
//  Created by xu yao on 2021/3/5.
//

#import "UGZRCpay.h"
#import "EnvironmentDefine.h"
#import "UGZRCpaySDK.h"
#import "UGZRCpayModel.h"
//#import <GZRCpaySDK/GZRCpaySDK.h>
#import "UPaySDK.h"
#import "HCToast.h"
#import <UIKit/UIApplication.h>
static UGZRCpay *currentHelper = nil;
@implementation UGZRCpay

/**
 #pragma mark - 唤起支付必传字段
 @property (nonatomic,copy) NSString *Sign;     //加密的串
 @property (nonatomic,copy) NSString *SignType; //加密类型
 @property (nonatomic,copy) NSString *Token;    //订单信息、订单金额、店铺编号、客户编号、订单详情
 #pragma mark -（SDK版本v 1.0）唤起支付非必传字段
 @property (nonatomic,copy) NSString *Ext;      //附加字段
 */
- (void)gzrcpayModel:(UGZRCpayModel *)payModel callback:(USDKPayResultHandler)completionBlock {
    if (!self.gzrcpayClass) {
        LCLog(@"请先配置黔农云支付");
        return;
    }
    if (!payModel) {
        LCLog(@"黔农云支付model不能为空");
        return;
    }
    Class gzrcPayModelClass = NSClassFromString(@"GZRCpayModel");
    if (!gzrcPayModelClass) {
        return;
    }
//    GZRCpayModel *model = [[GZRCpayModel alloc] init];
//    model.AppNo = @"81788980201911059999999";    //应用编号
//    model.MerNo = @"SH3090000000009";    //一级商户编号 用来获取店铺的公私钥
//    model.Sign = @"EIcnbQt0GrPcIevcLJ9fohFwkGf/m9TLRk35fKSb4bCkq5nIgXZxXE6YNxwspBSoitHOeFky93iRRPaDC+AWbygLpCWnWu5jEb5XZ1fPluX63OR44K4YzssdgnSsfL5UgW1hP94O7KKQS4qiux5tOTIoOkTDqc/yH5agIQAAfcGtp857FZId+oRIXAcCGXXnxe0TXo/U3XvDByO6uklpjIyYCFBzjHW+VQYEtzTu/MDa3bmDxiOpq8u0dcCsJAeaObgSsr9puwCVSi55ibaQWeCI0Be0cabEQNnDe7+6wMf1/DtNIeAWuWzDOMQSax97QgTUYd4ulxhHDVwbgCfgWA==";     //加密的串
//    model.SignType = @"RSA"; //加密类型
//    model.Token = @"BUV6GnJJIDbq2OG03govWOsfTRPK3zqhzyqdx02x3kDs3zmR0OKcIwbMks6lW9lC0NtFCbFN3NVywcE9tk0FpJ47y1da/h3x1pZNVXK21EKUZYSq56Ww19d8S5jtZfHHe4a3x32OuBZ7C4L75JZjOQD6lnu9G+hlLJVRCQCrwBNDo2rdkk6WnPDQgFbpVkRbYVB3UPK82Wy0+nwIIFJnpzJjEe2p0LtbmN3t4lQZO4eWN6V9F7/pHbUITkDMVT7XkFYQ2RMjGPIed5GomXgAGlt+HWr4P2M44h1hB3TRnxHBgoOgty7zlqSIbwJuLfKXxmM55GmDaERNR9YAYL4fcQ==";    //订单信息、订单金额、店铺编号、客户编号、订单详情
//    model.Ext = @"test";      //附加字段，当前版本sdk不需要传
    id request = [[gzrcPayModelClass alloc] init];
    [request setValue:payModel.AppNo forKey:@"AppNo"];
    [request setValue:payModel.MerNo forKey:@"MerNo"];
    [request setValue:payModel.Sign forKey:@"Sign"];
    [request setValue:payModel.SignType forKey:@"SignType"];
    [request setValue:payModel.Ext forKey:@"Ext"];
    [request setValue:payModel.Token forKey:@"Token"];
    if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"GZRCPAY://"]]) {
        
        return;
      }
    [[[currentHelper.gzrcpayClass class] defaultService] payModel:request callback:^(id  _Nonnull responseObject) {
        if (completionBlock) {
            completionBlock(USDKPlatformTypeGZRCloud, [responseObject[@"resultStatus"] intValue] == 9000 ? UPaySuccess : UPayFail, responseObject[@"memo"]);
        }
    }];
}

+ (void)handleOpenURL:(NSURL *)url onPayResult:(USDKPayResultHandler)payResultHandle {
    [[[currentHelper.gzrcpayClass class] defaultService] paymentResult:url callback:^(id  _Nonnull responseObject) {
        [[HCToast shareInstance] showToast:responseObject[@"memo"]];
        if (payResultHandle) {
            payResultHandle(USDKPlatformTypeGZRCloud, [responseObject[@"resultStatus"] intValue] == 9000 ? UPaySuccess : UPayFail, responseObject[@"memo"]);
        }
        if ([responseObject[@"resultStatus"] intValue] == 9000 ) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [UPaySDK closecheckstand];
            });
        }
    }];
}

+ (instancetype)currentHelper {
    if (currentHelper == nil)
        currentHelper = [[UGZRCpay alloc] init];
    
    return currentHelper;
}


- (instancetype)init {
    if (self = [super init]) {
        
    }
    return self;
}

@end
